﻿using System;
using System.Net;
using System.Windows;
#if (WINRT)
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using nRoute.Behaviors.Interactivity;
#else
using System.Windows.Markup;
#endif

namespace nRoute.Components
{
    public static class Page
    {

#region Dependency Properties

        public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached(
            "Title", typeof(string), typeof(Page), null);

        public static readonly DependencyProperty SubTitleProperty = DependencyProperty.RegisterAttached(
            "SubTitle", typeof(string), typeof(Page), null);

        public static readonly DependencyProperty IconPathProperty = DependencyProperty.RegisterAttached(
            "IconPath", typeof(string), typeof(Page), null);

        public static readonly DependencyProperty PageCommandsProperty = DependencyProperty.RegisterAttached(
            "PageCommands", typeof(PageCommandCollection), typeof(Page), 
#if (WINRT)
             new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnSetPageCommandsCallback));
#else
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));
#endif

		public static readonly DependencyProperty PageCrumbsProperty = DependencyProperty.RegisterAttached(
            "PageCrumbs", typeof(PageCommandCollection), typeof(Page), 
#if (WINRT)
             new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnSetPageCrumbsCallback));
#else
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));
#endif

        public static readonly DependencyProperty PageMenuProperty = DependencyProperty.RegisterAttached(
            "PageMenu", typeof(MenuCommandCollection), typeof(Page), 
#if (WINRT)
             new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE, OnSetPageMenuCallback));
#else
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));
#endif
            

#endregion

#region Getters and Setters

        public static void SetTitle(DependencyObject dependencyObject, string title)
        {
            dependencyObject.SetValue(TitleProperty, title);
        }

        public static string GetTitle(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return Convert.ToString(dependencyObject.GetValue(TitleProperty));
        }

        public static void SetSubTitle(DependencyObject dependencyObject, string subTitle)
        {
            dependencyObject.SetValue(SubTitleProperty, subTitle);
        }

        public static string GetSubTitle(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return Convert.ToString(dependencyObject.GetValue(SubTitleProperty));
        }

        public static void SetIconPath(DependencyObject dependencyObject, string iconPath)
        {
            dependencyObject.SetValue(IconPathProperty, iconPath);
        }

        public static string GetIconPath(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return Convert.ToString(dependencyObject.GetValue(IconPathProperty));
        }
        
#if (WINRT)
        public static void SetPageCommands(FrameworkElement dependencyObject, PageCommandCollection commands)
#else
        public static void SetPageCommands(DependencyObject dependencyObject, PageCommandCollection commands)
#endif
        {
            dependencyObject.SetValue(PageCommandsProperty, commands);
        }
        
#if (WINRT)
        public static PageCommandCollection GetPageCommands(FrameworkElement dependencyObject)
#else
        public static PageCommandCollection GetPageCommands(DependencyObject dependencyObject)
#endif
        {
            return (dependencyObject != null) ? GetOrCreatePageCommands(dependencyObject) : null;
		}

        public static PageCommandCollection GetPageCommandsValue(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return dependencyObject.GetValue(PageCommandsProperty) as PageCommandCollection;
        }
        
#if (WINRT)
        public static void SetPageCrumbs(FrameworkElement dependencyObject, PageCommandCollection crumbs)
#else
        public static void SetPageCrumbs(DependencyObject dependencyObject, PageCommandCollection crumbs)
#endif
		{
			dependencyObject.SetValue(PageCrumbsProperty, crumbs);
		}

#if (WINRT)
        public static PageCommandCollection GetPageCrumbs(FrameworkElement dependencyObject)
#else
        public static PageCommandCollection GetPageCrumbs(DependencyObject dependencyObject)
#endif
        {
            return (dependencyObject != null) ? GetOrCreatePageCrumbs(dependencyObject) : null;
        }

        public static PageCommandCollection GetPageCrumbsValue(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return dependencyObject.GetValue(PageCrumbsProperty) as PageCommandCollection;
        }
        
#if (WINRT)
        public static void SetPageMenu(FrameworkElement dependencyObject, MenuCommandCollection menu)
#else
        public static void SetPageMenu(DependencyObject dependencyObject, MenuCommandCollection menu)
#endif
        {
            dependencyObject.SetValue(PageMenuProperty, menu);
        }

#if (WINRT)
        public static MenuCommandCollection GetPageMenu(FrameworkElement dependencyObject)
#else
        public static MenuCommandCollection GetPageMenu(DependencyObject dependencyObject)
#endif
        {
            return (dependencyObject != null) ? GetOrCreatePageMenu(dependencyObject) : null;
        }

        public static MenuCommandCollection GetPageMenuValue(DependencyObject dependencyObject)
        {
            if (dependencyObject == null) return null;
            return dependencyObject.GetValue(PageMenuProperty) as MenuCommandCollection;
        }
		 
#endregion

#region Handlers

#if (WINRT)

        private static void OnSetPageCommandsCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _targetObject = dependencyObject as FrameworkElement;
            if (_targetObject != null)
            {
                PageCommandCollection _behaviours = GetOrCreatePageCommands(_targetObject);
                ((IAttachedObject)_behaviours).Attach(_targetObject);
            }
        }

        private static void OnSetPageCrumbsCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _targetObject = dependencyObject as FrameworkElement;
            if (_targetObject != null)
            {
                PageCommandCollection _behaviours = GetOrCreatePageCrumbs(_targetObject);
                ((IAttachedObject)_behaviours).Attach(_targetObject);
            }
        }

        private static void OnSetPageMenuCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _targetObject = dependencyObject as FrameworkElement;
            if (_targetObject != null)
            {
                MenuCommandCollection _behaviours = GetOrCreatePageMenu(_targetObject);
                ((IAttachedObject)_behaviours).Attach(_targetObject);
            }
        }

#endif

#endregion

#region Helper

        private static PageCommandCollection GetOrCreatePageCommands(DependencyObject dependencyObject)
        {
            var _pageCommands = dependencyObject.GetValue(PageCommandsProperty) as PageCommandCollection;
            if (_pageCommands == null)
            {
                _pageCommands = new PageCommandCollection();
                dependencyObject.SetValue(PageCommandsProperty, _pageCommands);
            }

            return _pageCommands;
        }

        private static PageCommandCollection GetOrCreatePageCrumbs(DependencyObject dependencyObject)
        {
            var _pageCrumbs = dependencyObject.GetValue(PageCrumbsProperty) as PageCommandCollection;
            if (_pageCrumbs == null)
            {
                _pageCrumbs = new PageCommandCollection();
                dependencyObject.SetValue(PageCrumbsProperty, _pageCrumbs);
            }

            return _pageCrumbs;
        }

        private static MenuCommandCollection GetOrCreatePageMenu(DependencyObject dependencyObject)
        {
            var _pageMenu = dependencyObject.GetValue(PageMenuProperty) as MenuCommandCollection;
            if (_pageMenu == null)
            {
                _pageMenu = new MenuCommandCollection();
                dependencyObject.SetValue(PageMenuProperty, _pageMenu);
            }

            return _pageMenu;
        }

#endregion

    }
}